$OpenBSD: patch-xcalc_c,v 1.2 2017/04/23 17:15:16 espie Exp $
--- xcalc.c.orig	Sun Aug  3 19:59:00 1997
+++ xcalc.c	Sun Apr 23 19:13:19 2017
@@ -4,7 +4,7 @@
 
 #ifndef lint
 static char *rcsid_xcalc_c = "$XConsortium: xcalc.c,v 1.23 88/10/05 11:13:33 jim Exp $";
-#endif	lint
+#endif	/* lint */
 
 /*
  * xcalc.c  -  a hand calculator for the X Window system
@@ -33,6 +33,9 @@ static char *rcsid_xcalc_c = "$XConsortium: xcalc.c,v 
  */
 
 #include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <errno.h>
 #include <math.h>
 #include <signal.h>
 #include <X11/Xos.h>
@@ -44,9 +47,7 @@ static char *rcsid_xcalc_c = "$XConsortium: xcalc.c,v 
 /* program icon */
 #include "icon"
 
-extern int errno;
 
-
 /* constants used for setting up the calculator.  changing them would 
    probably be bad.  If you do, don't forget to change the values in the
    syntax routine */
@@ -114,7 +115,7 @@ char dispstr[32];
 struct _key {
            char   *st;
 	   int	  code;
-	   int    (*fun)();
+	   void    (*fun)();
            Window wid;
            short  x,y,width,height;
            int    fore,back;
@@ -172,9 +173,46 @@ struct _key *key;
 #define kBKSP  46
 
 
-int  oneop(),twoop(),clearf(),offf(),invf(),drgf(),eef();
-int  lparf(),rparf(),digit(),decf(),negf(),equf();
-
+void set_sizehint(XSizeHints *, int, int, char *);
+void PushNum(double);
+void XCalcError(char *identifier, ...);
+void Syntax(void);
+void SetupHPCalc(void);
+void SetupTICalc(void);
+void ResetCalc(void);
+void DrawDisplay(void);
+void DrawKey(int);
+void InvertKey(int);
+void Quit(void);
+void LetGoKey(int);
+void TypeChar(int);
+void digit(int);
+void bkspf(void);
+void decf(void);
+void eef(void);
+void clearf(void);
+void ClearStacks(void);
+void negf(void);
+void twoop(int);
+int PopOp();
+void PushOp(int);
+int isopempty(void);
+int priority(int);
+void invf(void);
+void memf(int);
+void oneop(int);
+void twof(int);
+void entrf(void);
+void equf(void);
+void lparf(void);
+void rparf(void);
+void offf(void);
+void nop(void);
+void rollf(void);
+void drgf(void);
+void RollNum(int);
+void Timer(long);
+extern void do_sr(int, char **, char *, int);
 /* "1/x", "x^2", "SQRT","CE/C", "AC",
    "INV", "sin", "cos", "tan",  "DRG",
    "e",   "EE",  "log", "ln",   "y^x",
@@ -202,8 +240,6 @@ struct _key tikeys[TINUMKEYS] = {
   {"EXC",kEXC,oneop},{"0",kZERO,digit},{".",kDEC,decf},
   {"+/-",kNEG,negf},{"=",kEQU,equf}};
 
-int  twof(),nop(),rollf(),bkspf(),entrf(),memf();
-
 /*  { "SQRT","e^x", "10^x", "y^x", "1/x", "CHS", "7", "8", "9",  "/",
       "x!",  "PI",  "sin",  "cos", "tan", "EEX", "4", "5", "6",  "x",
       "",    "",    "R v",  "x:y", "<-",  "ENTR","1", "2", "3",  "-",
@@ -297,6 +333,7 @@ static void open_the_display (argc, argv)
 
 
 /**************/
+int
 main(argc, argv)
     int   argc;
     char *argv[];
@@ -689,7 +726,7 @@ main(argc, argv)
 	  }
         default:
             fprintf (stderr, "%s:  unexpected event type %ld = 0x%lx\n",
-	   	    ProgramName, event.type, event.type);
+	   	    ProgramName, (long)event.type, (long)event.type);
 	    break;
         }  /* end of switch */
     }  /* end main loop */}
@@ -697,6 +734,7 @@ main(argc, argv)
 
 
 /***********************************/
+void
 Syntax ()
 {
     fprintf (stderr, "usage:  %s [-options ...]\n\n",
@@ -723,16 +761,20 @@ Syntax ()
 
 
 /***********************************/
-XCalcError(identifier,arg1,arg2,arg3,arg4)
-       char *identifier,*arg1,*arg2,*arg3,*arg4;
+void
+XCalcError(char *identifier, ...)
 {
-    fprintf(stderr, identifier, arg1,arg2,arg3,arg4);
+    va_list va;
+    va_start(va, identifier);
+    vfprintf(stderr, identifier, va);
+    va_end(va);
     exit(1);
 }
 
 
 
 /***********************************/
+void
 SetupTICalc()
 {
     int i;
@@ -774,6 +816,7 @@ SetupTICalc()
 				  dispwide-2,disphigh,2,DispFore,DispBack);
 }
 
+void
 SetupHPCalc()
 {
     int i,j;
@@ -817,6 +860,7 @@ SetupHPCalc()
 
 
 /**************/
+void
 DrawDisplay()
 {
     int strwide;
@@ -827,11 +871,11 @@ DrawDisplay()
         if (!exp) dispstr[12]='\0';      /* no exp, just trunc. */
         else {
             if (strlen(exp)<=4) 
-                sprintf(tmp,"%.8s",dispstr); /* leftmost 8 chars */
+                snprintf(tmp, sizeof tmp, "%.8s",dispstr); /* leftmost 8 chars */
             else
-                sprintf(tmp,"%.7s",dispstr); /* leftmost 7 chars */
-            strcat (tmp,exp);            /* plus exponent */
-            strcpy (dispstr,tmp);
+                snprintf(tmp,sizeof tmp, "%.7s",dispstr); /* leftmost 7 chars */
+            strlcat (tmp,exp, sizeof tmp);            /* plus exponent */
+            strlcpy (dispstr,tmp, sizeof dispstr);
             }
         }
 
@@ -856,6 +900,7 @@ DrawDisplay()
 
 
 /***************/
+void
 DrawKey(keynum)
     int keynum;
 {
@@ -877,6 +922,7 @@ DrawKey(keynum)
 
 
 /*********************************/
+void
 InvertKey(keynum)
     int keynum;
 {
@@ -911,6 +957,7 @@ static double dnum=0.0;
 static double mem[10] = { 0.0 };
 
 /*********************************/
+void
 LetGoKey(keynum)
      int keynum;
 {
@@ -981,6 +1028,7 @@ LetGoKey(keynum)
 }
 
 
+void
 digit(keynum)
      int keynum;
 {
@@ -989,7 +1037,7 @@ digit(keynum)
       switch (memop) {
       case kSTO: mem[*(key[keynum].st) - '0'] = dnum; break;
       case kRCL: dnum = mem[*(key[keynum].st) - '0'];
-		 sprintf(dispstr, "%.8g", dnum);
+		 snprintf(dispstr, sizeof dispstr, "%.8g", dnum);
 	  	 break;
       case kSUM: mem[*(key[keynum].st) - '0'] += dnum; break;
       }
@@ -1006,13 +1054,14 @@ digit(keynum)
   }
   if (strlen(dispstr)>=MAXDISP)
     return;
-  strcat(dispstr,key[keynum].st);
+  strlcat(dispstr,key[keynum].st, sizeof dispstr);
   DrawDisplay();
   if (clrdisp && key[keynum].code != kZERO)
     clrdisp=0; /*no leading 0s*/
   entered=1;
 }
 
+void
 bkspf()
 {
   if (entered!=1 || clrdisp)
@@ -1026,6 +1075,7 @@ bkspf()
   DrawDisplay();
 }
 
+void
 decf()
 {
   flagINV=0;
@@ -1043,6 +1093,7 @@ decf()
   entered=1;
 }
 
+void
 eef()
 {
   flagINV=0;
@@ -1060,6 +1111,7 @@ eef()
   entered=1;
 }
 
+void
 clearf()
 {
   flagINV=0;
@@ -1075,6 +1127,7 @@ clearf()
   DrawDisplay();
 }
 
+void
 negf()
 {
   flagINV=0;
@@ -1090,11 +1143,11 @@ negf()
   if (strcmp("0",dispstr)==0)
     return;			/* don't neg a zero */
   if (dispstr[0]=='-')	 	/* already neg-ed */
-    strcpy(dispstr,dispstr+1);  /* move str left once */
+    memmove(dispstr, dispstr+1, sizeof dispstr-1); /* move str left once */
   else {			/* not neg-ed.  add a '-' */
     char tmp[32];
-    sprintf(tmp,"-%s",dispstr);
-    strcpy(dispstr,tmp);
+    snprintf(tmp, sizeof tmp, "-%s",dispstr);
+    strlcpy(dispstr,tmp, sizeof dispstr);
   }
   if (entered==2)
     dnum = -1.0 * dnum;
@@ -1102,7 +1155,8 @@ negf()
 }
 
 /* Two operand functions for infix calc */
-twoop(keynum)
+void
+twoop(int keynum)
 {
   double PopNum();
 
@@ -1155,7 +1209,7 @@ twoop(keynum)
 	}
       PushNum(acc);
       PushOp(key[keynum].code);
-      sprintf(dispstr,"%.8g",acc);
+      snprintf(dispstr, sizeof dispstr, "%.8g",acc);
       DrawDisplay();
       dnum=acc;
     }
@@ -1167,7 +1221,8 @@ twoop(keynum)
 }                      
 
 /* Two operand functions for rpn calc */
-twof(keynum)
+void
+twof(int keynum)
 {
   double PopNum();
 
@@ -1188,7 +1243,7 @@ twof(keynum)
   case kPOW: acc =  pow(acc,dnum);  break;
   case kXXY: PushNum(dnum);
   }
-  sprintf(dispstr, "%.8g", acc);
+  snprintf(dispstr, sizeof dispstr, "%.8g", acc);
   DrawDisplay();
   clrdisp++;
   decimal = exponent = 0;
@@ -1197,6 +1252,7 @@ twof(keynum)
 }
 
 
+void
 entrf()
 {
   flagINV=0;
@@ -1214,6 +1270,7 @@ entrf()
     PushNum(dnum);
 }
 
+void
 equf()
 {
   double PopNum();
@@ -1254,10 +1311,11 @@ equf()
     PushNum(dnum);
   }
 
-  sprintf(dispstr,"%.8g",dnum);
+  snprintf(dispstr, sizeof dispstr, "%.8g",dnum);
   DrawDisplay();
 }
         
+void
 lparf()
 {
   flagINV=0;
@@ -1266,6 +1324,7 @@ lparf()
   DrawDisplay();
 }
 
+void
 rollf()
 {
   double PopNum();
@@ -1278,10 +1337,11 @@ rollf()
   RollNum(flagINV);
   flagINV=0;
   clrdisp++;
-  sprintf(dispstr, "%.8g", dnum);
+  snprintf(dispstr, sizeof dispstr, "%.8g", dnum);
   DrawDisplay();
 }
 
+void
 rparf()
 {
   double PopNum();
@@ -1323,10 +1383,11 @@ rparf()
   PopNum();
   flagPAREN--;
   entered=2;
-  sprintf(dispstr,"%.8g",dnum);
+  snprintf(dispstr, sizeof dispstr, "%.8g",dnum);
   DrawDisplay();
 }
 
+void
 drgf()
 {
   if (flagINV) {
@@ -1340,11 +1401,12 @@ drgf()
     entered=2;
     clrdisp=1;
     flagINV=0;
-    sprintf(dispstr,"%.8g",dnum);
+    snprintf(dispstr, sizeof dispstr, "%.8g",dnum);
   }
                          
   flagINV=0;
-  drgmode = ++drgmode % 3;
+  ++drgmode;
+  drgmode %= 3;
   switch (drgmode) {
   case DEG:  drg2rad=PI / 180.0;
 	     rad2drg=180.0 / PI;
@@ -1359,13 +1421,15 @@ drgf()
   DrawDisplay();
 }
 
+void
 invf()
 {
   flagINV = ~flagINV;
   DrawDisplay();
 }
 
-memf(keynum)
+void
+memf(int keynum)
 {
     if (entered==1)
       parse_double(dispstr,"%lf",&dnum);
@@ -1373,7 +1437,8 @@ memf(keynum)
     clrdisp++;
 }
 
-oneop(keynum)
+void
+oneop(int keynum)
 {
   int i,j;
   double dtmp;
@@ -1431,10 +1496,11 @@ oneop(keynum)
   entered=2;
   clrdisp=1;
   flagINV=0;
-  sprintf(dispstr,"%.8g",dnum);
+  snprintf(dispstr, sizeof dispstr, "%.8g",dnum);
   DrawDisplay();
 }
 
+void
 offf()
 {
   /* full reset */
@@ -1446,6 +1512,7 @@ offf()
 }
 
 
+void
 nop()
 {
   XBell(dpy, 0);
@@ -1453,6 +1520,7 @@ nop()
 
 
 /*******/
+void
 Quit()
 /*******/
 {
@@ -1469,6 +1537,7 @@ static int numsp;
 
 
 /*******/
+void
 PushOp(op)
    int op;
 /*******/
@@ -1497,6 +1566,7 @@ int isopempty()
 }
 
 /*******/
+void
 PushNum(num)
  double num;
 /*******/
@@ -1533,7 +1603,8 @@ double PopNum()
 }
 
 /*******/
-RollNum(dir)
+void
+RollNum(int dir)
 /*******/
 {
     double tmp;
@@ -1563,6 +1634,7 @@ int isnumempty()
 
 
 /*******/
+void
 ClearStacks()
 /*******/
 {
@@ -1589,6 +1661,7 @@ int priority(op)
 
 
 /********/
+void
 ResetCalc()
 /********/
 {
@@ -1601,8 +1674,9 @@ ResetCalc()
 
 
 /*********/
+void
 TypeChar(c)
-    char c;
+char c;
 /*********/
 {
     /* figure out if person typed a valid calculator key.
@@ -1686,6 +1760,7 @@ onalarm()
 }
 
 /*******/
+void
 Timer(val)
  long val;
 /*******/
@@ -1716,6 +1791,7 @@ fperr(sig,code,scp)
 
 #endif
 
+void
 set_sizehint (hintp, min_width, min_height, geom)
     XSizeHints *hintp;
     int min_width, min_height;
